package com.itany.cloud.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author 石小俊
 * 全局过滤器
 */
//@Component
public class AuthenticationFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String company = exchange.getRequest().getQueryParams().getFirst("company");
        if("itany".equals(company)){
            // 放行
            return chain.filter(exchange);
        }
        // 如果企业不是itany,则表示鉴权失败
        // 设置响应状态码为401
        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        return exchange.getResponse().setComplete();
    }

    /**
     * 定义当前全局过滤器的优先级
     * 根据该方法的返回值判断优先级
     * 值越小,优先级越高
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
